package com.mango.wall.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.mango.config.BaseWall;
import com.mango.imageloader.core.DisplayImageOptions;
import com.mango.imageloader.core.display.RoundedBitmapDisplayer;
import com.mango.request.beans.AdvertBean;
import com.mango.wall.R;

import java.util.Locale;

public class InstallCompleteDialog extends Dialog implements View.OnClickListener, Runnable {
    private final AdvertBean mAdvert;
    private final DisplayImageOptions options
            = new DisplayImageOptions.Builder()
            .showImageOnFail(R.drawable.mg_wall_ic_default)
            .showImageOnLoading(R.drawable.mg_wall_ic_default)
            .showImageForEmptyUri(R.drawable.mg_wall_ic_default)
            .displayer(new RoundedBitmapDisplayer((int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 5, BaseWall.getInstance()
                            .getApplication().getResources().getDisplayMetrics()))).build();

    private Handler mHandler;
    private OnConfirmListener mListener;


    public InstallCompleteDialog(Context context, AdvertBean advert) {
        super(context, R.style.ManGoWall_Dialog);
        setCanceledOnTouchOutside(false);
        setCancelable(false);

        mAdvert = advert;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mg_wall_dialog_install);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = metrics.widthPixels;
        getWindow().setAttributes(params);

        ImageView img = findViewById(R.id.mg_wall_img);
        BaseWall.getInstance().getImageLoader().displayImage(
                mAdvert.adBaseInfo.appIconUrl, img, options);

        TextView txt = findViewById(R.id.mg_wall_txt);
        txt.setText(String.format(Locale.getDefault(),
                "%s安装成功", mAdvert.adBaseInfo.appName));

        findViewById(R.id.mg_wall_open).setOnClickListener(this);
        findViewById(R.id.mg_wall_close).setOnClickListener(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && mHandler == null) {
            mHandler = new Handler();
            mHandler.postDelayed(this, 1500);
        }
    }

    @Override
    public void onClick(View v) {
        if (mHandler != null) mHandler.removeCallbacks(this);
        if (isShowing()) cancel();
        if (mListener != null && v.getId()
                == R.id.mg_wall_open) mListener.onConfirm();
    }

    @Override
    public void run() {
        if (isShowing()) cancel();
        if (mListener != null) mListener.onConfirm();
    }

    public InstallCompleteDialog setOnConfirmListener(OnConfirmListener listener) {
        mListener = listener;
        return this;
    }

    public interface OnConfirmListener {
        void onConfirm();
    }
}
